package com.pogodin.wave;

import java.awt.Point;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class FrogDijkstra {

    private static int [][] arr;


    public static void main(String [] args){
        Random rand = new Random();
        //rand.nextInt(3) + 2
        arr = new int[5][5];

        for(int i = 0; i < arr.length; i++){
            for(int j = 0; j < arr[0].length; j++){
                arr[i][j] = rand.nextInt(10);
            }
        }

        System.out.println(Arrays.deepToString(arr));
        System.out.println(new FrogDijkstra().resolveTask(0, 0, -1, -1));

    }

    enum Step {
        LEFT, RIGHT, DOWN, UP
    }

    private int resolveTask(int x, int y, int prevX, int prevY){

        //represents possible ways from current (X,Y cell-coordinate) place
        Map<Integer, Point> nextSteps = new HashMap<Integer, Point>();

        if(x - 1 > -1 && prevX != x - 1){
            arr[x - 1][y] += arr[x][y];
            nextSteps.put(arr[x - 1][y], new Point(x - 1, y));
        }
        if(x + 1 < arr.length && prevX != x + 1){
            arr[x + 1][y] += arr[x][y];
            nextSteps.put(arr[x + 1][y], new Point(x + 1, y));
        }
        if(y - 1 > -1 && prevY != y - 1){
            arr[x][y - 1] += arr[x][y];
            nextSteps.put(arr[x][y - 1], new Point(x, y - 1));
        }
        if(y + 1 < arr[0].length && prevY != y + 1){
            arr[x][y + 1] += arr[x][y];
            nextSteps.put(arr[x][y + 1], new Point(x, y + 1));
        }

        int nextX = -1;
        int nextY = -1;
        int minimalSumm = -1;
        for (Integer currentSumm : nextSteps.keySet()) {
            if(currentSumm < minimalSumm || minimalSumm < 0){
                minimalSumm = currentSumm;
                nextX = (int)nextSteps.get(currentSumm).getX();
                nextY = (int)nextSteps.get(currentSumm).getY();
            }
        }

        System.out.println(nextX + " " + nextY);

        if(nextX == arr.length - 2 && nextY == arr[0].length - 1 ||
                nextX == arr.length - 1 && nextY == arr[0].length - 2){
            return arr[nextX][nextY] + arr[arr.length - 1][arr[0].length - 1];
        } else {
            return resolveTask(nextX, nextY, x, y);
        }

    }
}
